package com.itheima.service.impl;

import com.itheima.aop.Log;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> list() {
        List<Dept> list = deptMapper.list();

        return list;
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);

    }

    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    //@Transactional
    public void delectById(Integer id) {
        DeptLog deptLog = new DeptLog(null, LocalDateTime.now(), "删除" + id + "部门");
        deptLogService.insert(deptLog);
        deptMapper.deleteById(id);
      //  int i = 10 / 0;//模拟错误
        empMapper.deleteByDeptId(id);
    }

    /**
     * 修改部门
     *
     * @param dept
     */
    @Override
    public void update(Dept dept) {
        //  dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Log
    @Override
    public Dept GetById(Integer id) {
        Dept dept = deptMapper.GetById(id);
        return dept;
    }

}
